package cn.cmcc.online.smsapi.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.os.Process;
import cn.cmcc.online.smsapi.sdk.action.AbsSdkDoAction;
import cn.cmcc.online.smsapi.sdk.constant.Constant;
import cn.cmcc.online.smsapi.sdk.db.base.BaseManager;
import cn.cmcc.online.smsapi.sdk.db.entity.MatchCacheManager;
import cn.cmcc.online.smsapi.sdk.log.LogManager;
import cn.cmcc.online.smsapi.sdk.net.util.SigntureUtil;
import cn.cmcc.online.smsapi.sdk.queue.BubbleTaskQueue;
import cn.cmcc.online.smsapi.sdk.threadpool.SmartSmsThreadPoolManager;
import cn.cmcc.online.smsapi.sdk.util.DuoquUtils;
import cn.cmcc.online.smsapi.sdk.util.JsonUtil;
import cn.cmcc.online.smsapi.sdk.util.SdkCache;
import cn.cmcc.online.smsapi.sdk.util.StringUtils;
import cn.cmcc.online.smsapi.sdk.util.XyUtil;
/*SDK-616 hewengao 20160912 start*/
/*SDK-616 hewengao 20160912 end*/
/**
 * bubble mananer simple and rich bubble
 * 
 * @author Administrator
 * 
 */
public class ParseBubbleManager {

   
//    private static ExecutorService richPool = Executors.newFixedThreadPool(1);
//    private static long CACHETIME = 21600L;// 6个小时
    private static String currentPhoneNum =null;

    public static boolean equalPhoneNum(String phoneNum){
        if(phoneNum == null || currentPhoneNum == null){
//            LogManager.e("duoqu_test", "equalPhoneNum phoneNum: "+phoneNum+" currentPhoneNum: "+currentPhoneNum);
            return false;
        }
        phoneNum = StringUtils.getPhoneNumberNo86(phoneNum);
//        LogManager.e("duoqu_test", "equalPhoneNum phoneNum: "+phoneNum+" currentPhoneNum: "+currentPhoneNum);
        boolean bl = currentPhoneNum.equals(phoneNum);
//        LogManager.e("duoqu_test", "equalPhoneNum bl : "+bl);
        return bl;
    }

    public static void loadBubbleDataByPhoneNum(String phoneNum) {
        phoneNum = StringUtils.getPhoneNumberNo86(phoneNum);
        String where = "phoneNum=?";
        String[] args = new String[]{phoneNum};
        SdkCache sdkCache = SdkCache.createSdkCache(phoneNum);
        Map bubbleData = sdkCache.effectiveBubbleData;
        synchronized(sdkCache.effectiveBubbleData) {
            clearCacheBubbleData(phoneNum);
            currentPhoneNum = phoneNum;
        }

        bubbleData = MatchCacheManager.loadDataByParam(where, args, "save_time desc", "15");
        putBubbleDataToCache(sdkCache, bubbleData);
        ParseRichBubbleManager.pubBubbleData(phoneNum, bubbleData, true);
        loadBubbleDataByPhoneNumSecond(where,args,true,sdkCache,phoneNum);
    }


    /* SDK-615 zhaojiangwei 20160912 start*/
    public static void loadBubbleDataByPhoneNum(String phoneNum,boolean needRichData) {
        loadBubbleDataByPhoneNum(phoneNum, needRichData, false);
    }
    /* SDK-615 zhaojiangwei 20160912 end*/
    
    public static void loadBubbleDataByPhoneNum(String phoneNum,boolean needRichData, boolean needUrlData) {
//        LogManager.e("duoqu_test"," loadBubbleDataByPhoneNum:  phoneNum"+phoneNum+" needRichData: "+needRichData);
        if (StringUtils.isNull(phoneNum)) {
            return;
        }
        // async thread load data
        BeforeParseUtil.setStop();
        phoneNum = StringUtils.getPhoneNumberNo86(phoneNum);
        String where = "phoneNum=?";
        String args[] = new String[] { phoneNum };
        SdkCache sdkCache = SdkCache.createSdkCache(phoneNum);
        synchronized (sdkCache.effectiveBubbleData) {
            clearCacheBubbleData(phoneNum);
            currentPhoneNum =phoneNum;
//            LogManager.e("duoqu_test"," loadBubbleDataByPhoneNum:  currentPhoneNum"+currentPhoneNum);
        }
        // cacheBubbleData=MatchCacheManager.loadDataByParam(where,args);
        Map<String, JSONObject> bubbleData= MatchCacheManager.loadDataByParam(where, args,"save_time desc", "15");
        /*SDK-492 hewengao 20160616 start*/
        putBubbleDataToCache(sdkCache,bubbleData);
        /*SDK-492 hewengao 20160616 end*/
        if (needRichData) {
            ParseRichBubbleManager.pubBubbleData(phoneNum,bubbleData,true);
        }
          
        loadBubbleDataByPhoneNumSecond(where,args,needRichData,sdkCache,phoneNum);
        /* SDK-561 huangzhiqiang 20160725 start */
        ParseSmsToBubbleUtil.beforeHandParseReceiveSms(phoneNum, getBeforeHandParseSize(), 3, true);
        /* SDK-561 huangzhiqiang 20160725 end */
    }

    /* SDK-561 huangzhiqiang 20160725 start */
    /**
     * 获取会话预识别短信数量，默认值为50
     * 
     * @return
     */
    private static int getBeforeHandParseSize() {
        int defaultParseSize = 50;
        try {
            String strBeforeHandParseSize = DuoquUtils.getSdkDoAction()
                    .getConfig(AbsSdkDoAction.CONFIG_BEFORE_HAND_PARSE_SIZE, null);
            if (StringUtils.isNull(strBeforeHandParseSize)) {
                return defaultParseSize;
            }
            int beforeHandParseSize = Integer.parseInt(strBeforeHandParseSize);
            if (beforeHandParseSize < 0) {
                beforeHandParseSize = 0;
            } else if (beforeHandParseSize > 1000) {
                beforeHandParseSize = 1000;
            }
            return beforeHandParseSize;
        } catch (Throwable e) {
            LogManager.e(Constant.TAG, "getBeforeHandParseSize error", e);
        }
        return defaultParseSize;
    }
    /* SDK-561 huangzhiqiang 20160725 end */

    public static void loadBubbleDataByPhoneNumSecond(String where,String args[],
            boolean needRichData, SdkCache sdkCache,String phone) {
        // async thread load data
        try {
            Thread.sleep(10);
        }catch (InterruptedException e) {
//            e.printStackTrace();
            LogManager.e(Constant.TAG, "loadBubbleDataByPhoneNumSecond: ", e);

        }
        Map<String, JSONObject> bubbleData= MatchCacheManager.loadDataByParam(where, args,
                "save_time desc", "500");
        /*SDK-492 hewengao 20160616 start*/
        putBubbleDataToCache(sdkCache,bubbleData);
        /*SDK-492 hewengao 20160616 end*/
        if (needRichData) {
            ParseRichBubbleManager.pubBubbleData(phone,bubbleData,false);
        	 
        }
    }
    /*SDK-492 hewengao 20160616 start*/
    private static void putBubbleDataToCache(SdkCache sdkCache,Map<String, JSONObject> bubbleData){
		if (sdkCache == null) {
			return;
		}
		if (bubbleData != null) {
			synchronized (sdkCache.cacheBubbleData) {
				sdkCache.cacheBubbleData.putAll(bubbleData);
			}
			synchronized (sdkCache.cacheRecognisedValue) {
				sdkCache.cacheRecognisedValue.putAll(bubbleData);
			}
		}
    }
    /*SDK-492 hewengao 20160616 end*/
    public static void clearCacheBubbleData(String phoneNum) {
        clearAllCache(false,phoneNum);
    }
    public static void addEffectiveBubbleData(String phoneNum,String msgId,JSONArray actionData) {
            if(msgId == null || actionData ==null ){
                return;
            }
            SdkCache sdkCache =  SdkCache.getSdkCache(phoneNum);
            if(sdkCache == null){
            	return;
            }
            synchronized (sdkCache.effectiveBubbleData) {
            	sdkCache.effectiveBubbleData.put(msgId, actionData);
            }
            synchronized (sdkCache.cacheBubbleData) {
            	sdkCache.cacheBubbleData.remove(msgId);
            }
            synchronized (sdkCache.invalidBubbleData) {
            	sdkCache.invalidBubbleData.remove(msgId);
            }
            synchronized (sdkCache.inQueueBubbleData) {
            	sdkCache.inQueueBubbleData.remove(msgId);
                
            }
    }
    public static void addInvalidBubbleData(String phoneNum,String msgId) {
        if(msgId == null){
            return;
        }
        SdkCache sdkCache =  SdkCache.getSdkCache(phoneNum);
        if(sdkCache == null){
        	return;
        }
        synchronized (sdkCache.invalidBubbleData) {
        	sdkCache.invalidBubbleData.add(msgId);
        }
        synchronized (sdkCache.cacheBubbleData) {
        	sdkCache.cacheBubbleData.remove(msgId);
        }
        synchronized (sdkCache.inQueueBubbleData) {
        	sdkCache.inQueueBubbleData.remove(msgId);
        }
        synchronized (sdkCache.effectiveBubbleData) {
        	sdkCache.effectiveBubbleData.remove(msgId);
        }

}
    /*SDK-738 zhaojiangwei 20170322 start */
    public static void clearAllCache(String phoneNum) {
        clearAllCache(false, phoneNum);
    }
    
    public static void clearAllCache(boolean beforeParse,String phoneNum) {
        SdkCache.delSdkCache(phoneNum);
        if(beforeParse){
        ParseSmsToBubbleUtil.beforeHandParseReceiveSms(200, 3);
        }
        currentPhoneNum = null;
    }
    /*SDK-738 zhaojiangwei 20170322 end */

    public static JSONObject queryDataByMsgItem(String msgId, String phoneNum,
            String smsContent, String centerNum, int dataType,
            long smsReceiveTime) throws Exception {
        if (msgId == null || phoneNum == null || smsContent == null) {
            throw new Exception(
                    "msgid or phoneNum or smsContent is null but they need value.");
        }

        if (StringUtils.isPhoneNumber(phoneNum))
            return null;
        JSONObject res = null;
        SdkCache sdkCache =  SdkCache.createSdkCache(phoneNum);
        if (sdkCache.effectiveBubbleData1 != null) {
            res = sdkCache.effectiveBubbleData1.get(msgId);
            if (res != null) {
                return res;
            }
        }
        if (res == null && sdkCache.invalidBubbleData.contains(msgId)) {
            return null;
        }
        int dataStatu = 0;
        if (res == null && sdkCache.cacheBubbleData != null) {
            res = sdkCache.cacheBubbleData.get(msgId);
            if (res != null && !res.has("need_parse_simple")) {
                String md5 = (String) JsonUtil.getValueFromJsonObject(res,
                        "msg_num_md5");
                String md5_1 = MatchCacheManager.getMD5(phoneNum, smsContent);
                if (md5_1 != null && md5 != null && md5_1.equals(md5)) {
                    // find cache and data is same
                    if (dataType == 1) {// simple bubble data
//                        String data = (String) JsonUtil.getValueFromJsonObject(
//                                res, "session_reuslt");

                        String data = res.optString("session_reuslt");
                        try {
                            if (!StringUtils.isNull(data)) {
                                JSONArray dataArr = new JSONArray(data);
                                res.put("session_reuslt", dataArr);
                                sdkCache.effectiveBubbleData1.put(msgId, res);
                                dataStatu = 1;
                            } else {
                            	sdkCache.invalidBubbleData.add(msgId);// invalid data
                                    dataStatu = 2;
                            }
                        } catch (Throwable e) {
//                            e.printStackTrace();
                            LogManager.e(Constant.TAG, "queryDataByMsgItem: ", e);

                            sdkCache.invalidBubbleData.add(msgId);// invalid data
                            dataStatu = 2;

                        }
                    } else if (dataType == 2) {// rich bubble data

                    }
                } else {
                    // find cache but data is not same
                    // invalidBubbleData.add(msgId);
                    dataStatu = 3;
                }
                sdkCache.cacheBubbleData.remove(msgId);
            }
        }
        // /if(dataStatu == 0){

        // }
        if (dataStatu != 1) {
            if (sdkCache.inQueueBubbleData.contains(msgId)) {
                return null;
            } else {
            	sdkCache.inQueueBubbleData.add(msgId);
            }
            // 加入后台队列处理
            BubbleTaskQueue.addDataToQueue(dataStatu, msgId, phoneNum,
                    smsContent, centerNum, smsReceiveTime, dataType, res);
        }
        return res;
    }
    
    /*SDK-604 zhaojiangwei 20160901 start*/
    public static void queryDataByMsgItem(final String msgId,
            final String phoneNum, final String smsContent,
            final String centerNum, final int dataType,final long smsReceiveTime,
            final SdkCallBack xyCallBack, boolean scrollFing) throws Exception {
    	queryDataByMsgItem(msgId, phoneNum, smsContent, centerNum, dataType, smsReceiveTime, xyCallBack, scrollFing, null);
    }
    /*SDK-604 zhaojiangwei 20160901 end*/

    public static void queryDataByMsgItem(final String msgId,
            final String phoneNum, final String smsContent,
            final String centerNum, final int dataType,final long smsReceiveTime,
            final SdkCallBack xyCallBack, boolean scrollFing, final Map<String, String> extend) throws Exception {
        if (msgId == null || phoneNum == null || StringUtils.isNull(smsContent)) {
            XyUtil.doXycallBackResult(xyCallBack, -1, "phonenum is null",
                    msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
            return;
//            throw new Throwable("msgid or phoneNum or smsContent is null but they need value msgId: "+msgId);
        }
        if (StringUtils.isPhoneNumber(phoneNum)) {
            XyUtil.doXycallBackResult(xyCallBack, -1, "phonenum is null",
                    msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
            return;
        }
        JSONArray res = null;
        final SdkCache sdkCache = SdkCache.createSdkCache(phoneNum);
        if (sdkCache.effectiveBubbleData != null) {
            res = sdkCache.effectiveBubbleData.get(msgId);
            if (res != null) {
                XyUtil.doXycallBackResult(xyCallBack, 0, res,
                        msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                return;
            }

        }
        if (res == null && sdkCache.invalidBubbleData.contains(msgId)) {
            XyUtil.doXycallBackResult(xyCallBack, -1," invalidBubbleData ",
                    msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
            return;
        }
        int dataStatu = 0;
        if (res == null && sdkCache.cacheBubbleData != null) {
              JSONObject jsObj = sdkCache.cacheBubbleData.get(msgId);
//              LogManager.e("duoqu_test", "cacheBubbleData find msgId: "+msgId+" jsObj: "+jsObj);
              if (jsObj != null && !jsObj.has("need_parse_simple")) {
                  String md5 = (String) JsonUtil.getValueFromJsonObject(jsObj,"msg_num_md5");
                  String md5_1 = MatchCacheManager.getMD5(phoneNum, smsContent);
//                  LogManager.e("duoqu_test", "md5: "+md5);
                  if (md5_1 != null && md5 != null && md5_1.equals(md5)) {
                      try{    
//                         LogManager.e("duoqu_test", "md5equals: "+md5);
                          JSONArray dataArr = (JSONArray) JsonUtil.getValueFromJsonObject(jsObj, "session_reuslt");
                          if(dataArr != null){
                        	  sdkCache.effectiveBubbleData.put(msgId, dataArr);
                              dataStatu = 1;
                              XyUtil.doXycallBackResult(xyCallBack, 0,dataArr,
                                      msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                              return;
                          }else{
//                              XyUtil.doXycallBackResult(xyCallBack, -1," invalidBubbleData ", msgId);
                        	  sdkCache.invalidBubbleData.add(msgId);// invalid data
                              dataStatu = 2;
                          }
                     }catch(Throwable e){
//                         XyUtil.doXycallBackResult(xyCallBack, -1," invalidBubbleData ", msgId);
                    	 sdkCache.invalidBubbleData.add(msgId);// invalid data
                         dataStatu = 2;
//                         e.printStackTrace();
                         LogManager.e(Constant.TAG, "queryDataByMsgItem: ", e);

                     }
                  }else {
                      // find cache but data is not same
                      // invalidBubbleData.add(msgId);
                      dataStatu = 3;
                  }
                  sdkCache.cacheBubbleData.remove(msgId);
              }
        }
        if (dataStatu == 0 || dataStatu ==3) {
            if (sdkCache.inQueueBubbleData.contains(msgId)) {
                XyUtil.doXycallBackResult(xyCallBack, -1," inQueueBubbleData2 ",
                        msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                return;
            } else {
                // inQueueBubbleData.add(msgId);
            }

            if (!scrollFing) {
                /* huangzhiqiang 20160927 start */
                sdkCache.inQueueBubbleData.add(msgId);
                /* huangzhiqiang 20160927 end */
                XyUtil.doXycallBackResult(xyCallBack, -2, " need parse",
                        msgId,  ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                /*SDK-616 hewengao 20160912 start*/
                SmartSmsThreadPoolManager.richPool.execute(new Runnable() {
                /*SDK-616 hewengao 20160912 end*/
                    @Override
                    public void run() {
                        try {
                            /*SDK-616 hewengao 20160912 start*/
                            SmartSmsThreadPoolManager.setThreadNameAndPriority(SmartSmsThreadPoolManager.TNAME_RICHPOOL,
                                    Process.THREAD_PRIORITY_BACKGROUND);
                            /*SDK-616 hewengao 20160912 */
                            Process.setThreadPriority(Process.THREAD_PRIORITY_AUDIO);
                            JSONArray dataArr = null;
                            JSONObject res = ParseRichBubbleManager.queryBubbleDataFromDb(msgId, phoneNum, smsContent, smsReceiveTime);
                            if(res != null && !res.has("need_parse_simple")){

                                dataArr = (JSONArray) JsonUtil.getValueFromJsonObject(res, "session_reuslt");
                                if(dataArr != null){
                                    synchronized (sdkCache.effectiveBubbleData) {
                                    	sdkCache.effectiveBubbleData.put(msgId, dataArr);
                                    }
                                    sdkCache.invalidBubbleData.remove(msgId);
                                    XyUtil.doXycallBackResult(xyCallBack, 1, dataArr,
                                            msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                                    return;
                                }else{
                                	sdkCache.invalidBubbleData.add(msgId);// invalid data
                                    XyUtil.doXycallBackResult(xyCallBack, -3, " invalid data need_parse_simple", msgId,
                                            ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                                        return;
                                }
                            }
                            if(dataArr == null){
                                dataArr = querySimpleDataFromApi(msgId,
                                        phoneNum, centerNum, smsContent,smsReceiveTime, extend);
                            }
                            if (dataArr != null) {
                                synchronized (sdkCache.effectiveBubbleData) {
                                	sdkCache.effectiveBubbleData.put(msgId, dataArr);
                                }
                                XyUtil.doXycallBackResult(xyCallBack, 1,dataArr,
                                        msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                            }else{
                            	sdkCache.invalidBubbleData.add(msgId);
                                XyUtil.doXycallBackResult(xyCallBack, -3,"$$$$$$$$$$ dataType: " + dataType,
                                        msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                            }
                        } catch (Throwable e) {
                            LogManager.e("RichBubbleTaskQueue",e.getLocalizedMessage(),e );
                            XyUtil.doXycallBackResult(xyCallBack, -3,"result is null: error: " + e.getMessage(),
                                    msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
                        } finally {
                            /* huangzhiqiang 20160927 start */
                            sdkCache.inQueueBubbleData.remove(msgId);
                            /* huangzhiqiang 20160927 end */
                        }
                    }
                });
            } else {
                XyUtil.doXycallBackResult(xyCallBack, -4, " is scrolling",
                        msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
            }

        }else{
        	 XyUtil.doXycallBackResult(xyCallBack, -1, " invalidBubbleData ",
        	         msgId, ParseSmsMessage.DUOQU_SMARTSMS_SHOW_BUBBLE_SIMPLE_FLAG);
        }

    }
   
   /*SDK-604 zhaojiangwei 20160901 start*/
   private static JSONArray querySimpleDataFromApi(String msgId,String phoneNum,String centerNum,String smsContent,long smsReceiveTime, Map<String, String> extend){
       JSONArray dataArr =null;
       try {
           String result = ParseBubbleManager.parseMsgToSimpleBubbleResultKuai(
                   Constant.getContext(), msgId,
                   phoneNum, centerNum, smsContent,smsReceiveTime,
                   (byte) 1, extend);
           if (result != null) {
               dataArr = new JSONArray(result);
           }
       }catch (Throwable e) {
           LogManager.e(Constant.TAG, "querySimpleDataFromApi: ", e);
       }
       return dataArr;
   }
   /*SDK-604 zhaojiangwei 20160901 end*/
  
    public static String parseMsgToSimpleBubbleResultKuai(Context ctx,
            String msgId, String phoneNumber, String smsCenterNum,
            String smsContent,long smsReceiveTime, byte returnCacheType, Map<String, String> extend)
            throws Exception {       
        /* MEIZU-1820 zhaojiangwei 20161217 start */
        if (extend == null) {
			extend = new HashMap<String, String>();
		}
        extend.put("msgTime", String.valueOf(smsReceiveTime));
        /* MEIZU-1820 zhaojiangwei 20161217 end */
        String data = ParseManager.parseMsgToBubble(ctx, phoneNumber,
                smsCenterNum, smsContent, extend);
        saveSimpleResultToDb(msgId, phoneNumber, smsCenterNum, smsContent,smsReceiveTime, data);
        return data;
 
    }

//    static ExecutorService saveSimpleResultService = Executors
//            .newFixedThreadPool(1);

    static void saveSimpleResultToDb(final String msgId,
            final String phoneNumber, final String smsCenterNum,
            final String smsContent,final long smsReceiveTime,final String data) {
//        SmartSmsThreadPoolManager.saveSimpleResultService;
        /*sdk-616 hewengao/20160918 start*/
        SmartSmsThreadPoolManager.iccidPool.execute(new Runnable() {
        /*sdk-616 hewengao/20160918 end*/
            @Override
            public void run() {
            	try {
					String msg_num_md5 = MatchCacheManager.getMD5(phoneNumber,
                        smsContent);
                if (!StringUtils.isNull(msg_num_md5)) {
                    ContentValues matchCache = BaseManager.getContentValues(
                            null, "msg_num_md5", msg_num_md5, "phonenum",
                            StringUtils.getPhoneNumberNo86(phoneNumber),
                            "msg_id", msgId, "session_reuslt",
                            data == null ? "" : data, "save_time",
                            String.valueOf(smsReceiveTime),
                            "session_lasttime", String.valueOf(System.currentTimeMillis()));
                    MatchCacheManager.insertOrUpdate(matchCache, 1);
                }
				} catch (Throwable e) {
					LogManager.e(Constant.TAG, e.getMessage(), e);
				}
            }
        });

    }
    /**
     *
     * @param msgId
     * @param phone
     * @param msg
     * @return
     */
    public static boolean deleteBubbleData(String msgId,String phone,String msg){
    	String md5  =null;
    	if(phone != null){
    	    md5 =MatchCacheManager.getMD5(phone,msg);
    	}
    	deleteBubbleDataFromCache(phone,msgId);
    	ParseRichBubbleManager.deleteBubbleDataFromCache(phone,msgId);
    	return MatchCacheManager.deleteBubbleData(msgId, md5);
    }
    
    public static void deleteBubbleDataFromCache(String phoneNum,String msgId){
    	try {
	    	  SdkCache sdkCache =  SdkCache.getSdkCache(phoneNum);
	    	  if(sdkCache == null){  	
	    		  return;
    		  }
    		  sdkCache.cacheBubbleData.remove(msgId);
    		  sdkCache.effectiveBubbleData.remove(msgId);
    		  sdkCache.invalidBubbleData.remove(msgId);
    		  sdkCache.inQueueBubbleData.remove(msgId);
		} catch (Throwable e) {
			// TODO: handle Throwable
		}
    }
    
    public static int getParseStatu(Map<String, Object> parseValue){
	    if(parseValue != null ){
	    	Integer parseStatu = (Integer)parseValue.get("parseStatu");
	    	if(parseStatu != null && parseStatu == -1){
	    		//表示还未解压
	    		return -1;
	    	}
	    }
	    return 0;
    }
    public static int deleteBubbleData(String phoneNum){
    	try {
	    	  SdkCache sdkCache =  SdkCache.getSdkCache(phoneNum);
	    	  if(sdkCache != null){  	
	    		  sdkCache.clearSdkCache();
    		  }
    		return  MatchCacheManager.deleteDataByPhoneNum(phoneNum);
		} catch (Throwable e) {
			// TODO: handle Throwable
		}
    	return -1;
    }
    
    public static void deleteBubbleData(final Set<Integer> ids){
        try {
            if (ids != null && !ids.isEmpty()) {
                /*SDK-616 hewengao 20160912 start*/
                SmartSmsThreadPoolManager.iccidPool.execute(new Runnable() {
                /*SDK-616 hewengao 20160912 start*/
                    public void run() {
                    	try {
                    		Iterator<Integer> it = ids.iterator();  
                			while (it.hasNext()) {  
                				Integer msgId = it.next();  
                	        	ParseRichBubbleManager.deleteBubbleDataFromCache("",msgId+"");    	        	
                			}  
                			MatchCacheManager.deleteDataByMsgIds(ids);
						} catch (Throwable e) {
//							e.printStackTrace();
				            LogManager.e(Constant.TAG, "deleteBubbleData: ", e);

						}
                    	
                    }
                });
    		    
    			
    		}
    		
		} catch (Throwable e) {
//			e.printStackTrace();
            LogManager.e(Constant.TAG, "deleteBubbleData: ", e);

		}
    	
    }
}
